<?php
/**
 * Created by xiaokengge
 * User: zhaoyu
 * Date: 2024/1/16
 * Time: 15:01
 *describe:****
 */
namespace app\api\service;
use EasyWeChat\Factory;
use think\Cache;

class EasywechatService
{

    protected    $config = [
        'app_id' => 'wx56237a44263fb136',
        'secret' => '99711b4070c458c6804005a84b56a5b8',
        'verify'=>false,
        // 下面为可选项
        // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
        'response_type' => 'array',

//        'log' => [
//            'level' => 'debug',
//            'file' => __DIR__.'/wechat.log',
//        ],
    ];

    public  $app;


    public  function  __construct(){
//        (new ThrowNewErr())->validateFailException('异常');
        $app  = Factory::miniProgram($this->config);
        $this->app=$app;
    }

    public  function  loginCode($code){
       $res=$this->app->auth->session($code);

       if(isset($res['openid']) && !empty($res['openid'])){
            return $res;
       }else{
           (new ThrowNewErr())->validateFailException($res['errmsg']);
       }

    }


    //获取微信access_token
    public function getAccessToken()
    {
        $appid = $this->config['app_id'];
        $appsecret = $this->config['secret'];

//        $cache = Cache::get('wechat_access_token');
//        if (!isset($cache) || $cache == 'null') {
            $res = json_decode($this->httpGet('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret), true);
          var_dump($res);die;
            $access_token = $res['access_token'];
            if ($access_token) {
                Cache::set('wechat_access_token', $access_token, 3600);
            }
//        } else {
//            $access_token = Cache::get('wechat_access_token');
//        }
        return $access_token;
    }

    private function httpGet($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_URL, $url);

        $res = curl_exec($curl);
        curl_close($curl);

        return $res;
    }

    private function httpPost($url, $post_data = '', $timeout = 5)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        if ($post_data != '') {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HEADER, false);
        $file_contents = curl_exec($ch);
        curl_close($ch);

        return $file_contents;
    }

    public  function  getPhoneNumber($code){
        $accessToke= $this->getAccessToken();
        var_dump($accessToke);die;
        $url= 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$accessToke;
        $postData=[
            'code'=>$code,
        ];
       $res= $this->httpPost($url,json_encode($postData));
        var_dump($res);die;


    }



}
